﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using RTW.Models;
using RTW.Servers;
using RTW.Web.Comm;
using RTW.Web.Models;

namespace RTW.Web.Areas.System.Controllers
{
    [Area("System")]
    public class CategoryController : CommController
    {
        private readonly TCategoryServer categoryServer;

        public CategoryController()
        {
            categoryServer = new TCategoryServer();
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult CategoryManagement()
        {
            return View();
        }


        public async Task<IActionResult> CategoryInfoEdit()
        {
            IList<TCategory> list = await categoryServer.GetAsync();
            List<SelectListItem> listitem = new List<SelectListItem>
            {
                new SelectListItem { Text = "", Value = "" }
            };
            foreach (var dep in list)
            {
                listitem.Add(new SelectListItem
                {
                    Text = dep.CName,
                    Value = dep.ID
                });
            }
            ViewBag.Categorys = listitem;

            return View();
        }


        public IActionResult CategoryFieldEdit()
        {
            return View();
        }


        public IActionResult CategoryFormEdit()
        {
            return View();
        }

        public IActionResult CategoryViewEdit()
        {
            return View();
        }

        public IActionResult CategoryOutEdit()
        {
            return View();
        }

        public async Task<IActionResult> GetCategoryTreeJson()
        {
            var treelist = await categoryServer.GetAsync();
            List<JsTreeModel> trelist = new List<JsTreeModel>();

            //定义递归方法
            int GetChildrenModule(List<TCategory> clist, JsTreeModel jsTree)
            {
                var childrenmodules = clist;
                if (childrenmodules.Count() > 0)
                {
                    jsTree.children = new List<JsTreeModel>();
                    foreach (var cm in childrenmodules)
                    {
                        JsTreeModel cjsTree = new JsTreeModel();
                        cjsTree.id = cm.ID;
                        cjsTree.text =  cm.CName;
                        cjsTree.state = new Dictionary<string, bool>() { { "opened", true } };
                        cjsTree.li_attr = new Dictionary<string, string>() { { "class", "jstree-open" } };
                        cjsTree.a_attr = new Dictionary<string, string>() { { "DMID", cm.ID }, { "onclick", "GetTabData('" + cm.ID + "')" } };
                        GetChildrenModule(cm.CategoryS, cjsTree);
                        jsTree.children.Add(cjsTree);
                    }
                }
                return childrenmodules.Count();
            }

            //遍历根节点
            foreach (var item in treelist)
            {

                JsTreeModel jsTree = new JsTreeModel();
                jsTree.id = item.ID;
                jsTree.text = item.CName;
                jsTree.state = new Dictionary<string, bool>() { { "opened", true } };
                jsTree.li_attr = new Dictionary<string, string>() { { "class", "jstree-open" } };
                jsTree.a_attr = new Dictionary<string, string>() { { "DMID", item.ID }, { "onclick", "GetTabData('" + item.ID + "')" } };
                GetChildrenModule(item.CategoryS, jsTree);

                trelist.Add(jsTree);
            }

            return Json(trelist);
        }
    }
}
